home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / colorEdit.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  125 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1993, David Koblas (koblas@netcom.com)                  | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/Intrinsic.h>
  16. #include <X11/StringDefs.h>
  17. #include <X11/Shell.h>
  18. #include <X11/Xaw/Form.h>
  19. #include <X11/Xaw/Command.h>
  20. #include "misc.h"
  21. #include "palette.h"
  22.  
  23. typedef struct {
  24.     XtCallbackProc    okProc;
  25.     XtPointer    closure;
  26.     Widget        wIn;
  27.  
  28.     Widget        shell, pick;
  29.     Palette        *map;
  30.     Boolean        allowWrite;
  31.     XColor        origColor;
  32. } LocalInfo;
  33.  
  34. static void commonCB(LocalInfo *info, XColor *col)
  35. {
  36.     Widget        cw = info->wIn;
  37.     XtCallbackProc    proc = info->okProc;
  38.     XtPointer    closure = info->closure;
  39.  
  40.     XtDestroyWidget(info->shell);
  41.     XtFree((XtPointer)info);
  42.  
  43.     proc(cw, closure, (XtPointer)col);
  44. }
  45.  
  46. static void changeBgCancel(Widget w, LocalInfo *info, XtPointer junk2)
  47. {
  48.     if (info->allowWrite) 
  49.         PaletteSetPixel(info->map, ColorPickerGetPixel(info->pick), 
  50.                     &info->origColor);
  51.     commonCB(info, NULL);
  52. }
  53. static void changeBgOk(Widget w, LocalInfo *info, XtPointer junk2)
  54. {
  55.     XColor        xcol;
  56.  
  57.     xcol = *ColorPickerGetXColor(info->pick);
  58.  
  59.     commonCB(info, &xcol);
  60. }
  61.  
  62. void ColorEditor(Widget w, Pixel pixel, Palette *map, Boolean allowWrite,
  63.             XtCallbackProc okProc, XtPointer closure)
  64. {
  65.     extern Widget    ColorPickerPalette(Widget, Palette *, Pixel *);
  66.     Position    x, y;
  67.     Widget        shell, form, ok, cancel, pick;
  68.     Pixel        pix, bg;
  69.     LocalInfo    *info = XtNew(LocalInfo);
  70.  
  71.     info->okProc     = okProc;
  72.     info->closure    = closure;
  73.     info->wIn        = w;
  74.     info->allowWrite = allowWrite;
  75.  
  76.     XtVaGetValues(GetShell(w), XtNx, &x, XtNy, &y, NULL);
  77.  
  78.     shell = XtVaCreatePopupShell("colorEditDialog", transientShellWidgetClass, GetShell(w),
  79.                 XtNx, x + 24,
  80.                 XtNy, y + 24,
  81.                 XtNcolormap, map->cmap,
  82.                 NULL);
  83.     PaletteAddUser(map, shell);
  84.  
  85.     form  = XtVaCreateManagedWidget("form", formWidgetClass, shell, 
  86.             NULL);
  87.  
  88.     if (allowWrite) {
  89.         pix = pixel;
  90.     } else {
  91.         pix = PaletteGetUnused(map);
  92.     }
  93.     pick = ColorPickerPalette(form, map, &pix);
  94.     info->origColor = *PaletteLookup(map, pixel);
  95.     ColorPickerSetXColor(pick, &info->origColor);
  96.  
  97.     ok = XtVaCreateManagedWidget("ok", 
  98.                 commandWidgetClass, form,
  99.                 XtNfromVert, pick,
  100.                 XtNtop, XtChainBottom,
  101.                 XtNbottom, XtChainBottom,
  102.                 XtNleft, XtChainLeft,
  103.                 XtNright, XtChainLeft,
  104.                 NULL);
  105.  
  106.     cancel = XtVaCreateManagedWidget("cancel", 
  107.                 commandWidgetClass, form,
  108.                 XtNfromVert, pick,
  109.                 XtNfromHoriz, ok,
  110.                 XtNtop, XtChainBottom,
  111.                 XtNbottom, XtChainBottom,
  112.                 XtNleft, XtChainLeft,
  113.                 XtNright, XtChainLeft,
  114.                 NULL);
  115.     XtAddCallback(cancel, XtNcallback, (XtCallbackProc)changeBgCancel, (XtPointer)info);
  116.     XtAddCallback(ok, XtNcallback, (XtCallbackProc)changeBgOk, (XtPointer)info);
  117.         AddDestroyCallback(shell, (void (*)(Widget, void *, XEvent *))changeBgCancel, (XtPointer)info);
  118.  
  119.     info->shell = shell;
  120.     info->pick  = pick;
  121.     info->map   = map;
  122.  
  123.     XtPopup(shell, XtGrabNone);
  124. }
  125.